def set_stage():
""" Sets up the stage for the game """
stage.set_background("soccerfield")
stage.disable_floor()
def add_player():
""" Adds a player to the stage for the user to control """
player = codesters.Sprite("player1")
player.go_to(0, -180)
player.set_size(0.75)
return player
def add_ball():
""" Adds a ball to the stage and sets its attributes """
ball = codesters.Sprite("soccerball")
ball.set_y_speed(-8)
def head_ball(sprite, hit_sprite):
""" Detects collisions between the player and ball """
my_var = hit_sprite.get_y_speed()
hit_sprite.set_y_speed(-my_var + 1)
def main():
""" Sets up the program and calls other functions """
set_stage()
player = add_player()
add_ball()
player.event_collision(head_ball)
main()
t = codesters.Teacher()
defs = t.find_block("def")
get_x_speeds = t.find_function("get_x_speed")
set_x_speeds = t.find_function("set_x_speed")
def get_above_def(line_number, def_list):
""" Returns the name of the nearest function and the distance above the given line number """
function_name = ""
distance = line_number - def_list[0][0]
for func in def_list:
if line_number - func[0] <= distance and line_number - func[0] > 0:
function_name = func[1]
distance = line_number - func[0]
return function_name, distance
try:
tval1 = get_x_speeds[0][1].replace(' ','').lower()
tval1_line_num = get_x_speeds[0][0]
tval1_indent = t.get_indent_at_line(tval1_line_num)
except:
tval1 = "DNE"
tval1_line_num = "DNE"
tval1_indent = "DNE"
try:
tval1_def, distance = get_above_def(tval1_line_num, defs)
except:
tval1_def = "DNE"
distance ="DNE"
try:
tval2 = set_x_speeds[0][1].replace(' ','').lower()
tval2_line_num = set_x_speeds[0][0]
tval2_indent = t.get_indent_at_line(tval2_line_num)
except:
tval2 = "DNE"
tval2_line_num = "DNE"
tval2_indent = "DNE"
try:
tval2_def, distance = get_above_def(tval2_line_num, defs)
except:
tval2_def = "DNE"
distance ="DNE"
t1 = TestObjective()
t1.add_success("hit_sprite" in tval1, "Great job!")
t1.add_failure(tval1 == "DNE", "Did you add Get x Speed into the head_ball() function?")
t1.add_failure(tval1 != "DNE" and "hit_sprite" not in tval1, "Did you change sprite to hit_sprite in front of .get_x_speed()?")
t2 = TestObjective()
t2.add_success("head_ball" in tval1_def and tval1_indent == 4, "Great job!")
t2.add_failure("head_ball" not in tval1_def, "Did you place Get x Speed within the head_ball() function?")
t2.add_failure(tval1_indent < 4 or tval1_indent > 4, "Make sure Set y Speed is indented within head_ball()!")
t3 = TestObjective()
t3.add_success("hit_sprite" in tval2 and "(my_var+1)" in tval2, "Great job!")
t3.add_failure(tval2 == "DNE", "Did you add Set x Speed into the head_ball() function?")
t3.add_failure(tval2 != "DNE" and "hit_sprite" not in tval2, "Did you change sprite to hit_sprite in front of .set_x_speed()?")
t3.add_failure(tval2 != "DNE" and "(my_var+1)" not in tval2, "Did you add the correct equation in the parentheses of .set_x_speed()?")
t4 = TestObjective()
t4.add_success("head_ball" in tval2_def and tval2_indent == 4, "Great job!")
t4.add_failure("head_ball" not in tval2_def, "Did you place Set x Speed within the head_ball() function?")
t4.add_failure(tval2_indent < 4 or tval2_indent > 4, "Make sure Set x Speed is indented within head_ball()!")
tester = TestManager()
tester.add_test_list([t1, t2, t3, t4])
tester.run_tests()
tester.display_first_feedback()
-
Run Code
-
Activity Submitted!
Enviar Trabajo
-
Actividad Siguiente
-
Stop Running Code
-
Show Chart
-
Show Console
-
Reset Code Editor
-
Codesters How To (opens in a new tab)